package cn.iocoder.yudao.module.famous.doctor.controller.app.videohistory;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.videohistory.vo.*;
import cn.iocoder.yudao.module.famous.doctor.convert.videohistory.VideoHistoryConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.video.FamVideoDO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.videohistory.VideoHistoryDO;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.video.FamVideoMapper;
import cn.iocoder.yudao.module.famous.doctor.service.videohistory.VideoHistoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "App - 我的视频记录")
@RestController
@RequestMapping("/famous.doctor/video-history/app")
@Validated
public class AppVideoHistoryController {

    @Resource
    private VideoHistoryService videoHistoryService;
    @Resource
    private FamVideoMapper famVideoMapper;

    @PostMapping("/create")
    @Operation(summary = "创建我的视频记录")
    public CommonResult<Long> createVideoHistory(@Valid @RequestBody VideoHistoryCreateReqVO createReqVO) {
        return success(videoHistoryService.createVideoHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的视频记录")
    public CommonResult<Boolean> updateVideoHistory(@Valid @RequestBody VideoHistoryUpdateReqVO updateReqVO) {
        videoHistoryService.updateVideoHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的视频记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteVideoHistory(@RequestParam("id") Long id) {
        videoHistoryService.deleteVideoHistory(id);
        return success(true);
    }

    @DeleteMapping("/deletebatch")
    @Operation(summary = "批量删除我的视频记录")
    @Parameter(name = "ids", description = "编号", required = true)
    public CommonResult<Boolean> deletebatchVideoHistory(@RequestParam("ids") Collection<Long> ids) {
        videoHistoryService.deletebatchVideoHistory(ids);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "id获得视频记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<VideoHistoryRespVO> getVideoHistory(@RequestParam("id") Long id) {
        VideoHistoryDO videoHistory = videoHistoryService.getVideoHistory(id);
        return success(VideoHistoryConvert.INSTANCE.convert(videoHistory));
    }



    @GetMapping("/getMessage")
    @Operation(summary = "获得我的视频记录")
    public CommonResult<IPage<VideoMyHistoryRespVO>> getMessage(@Valid PageParam param) {
        return success(videoHistoryService.getMessage(param));
    }


    @GetMapping("/list")
    @Operation(summary = "获得我的视频记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<VideoHistoryRespVO>> getVideoHistoryList(@RequestParam("ids") Collection<Long> ids) {
        List<VideoHistoryDO> list = videoHistoryService.getVideoHistoryList(ids);
        return success(VideoHistoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的视频记录分页")
    public CommonResult<PageResult<VideoHistoryRespVO>> getVideoHistoryPage(@Valid VideoHistoryPageReqVO pageVO) {
        PageResult<VideoHistoryDO> pageResult = videoHistoryService.getVideoHistoryPage(pageVO);
        PageResult<VideoHistoryRespVO> videoHistoryRespVOPageResult = VideoHistoryConvert.INSTANCE.convertPage(pageResult);
        videoHistoryRespVOPageResult.getList().forEach(videoHistoryRespVO -> {
            FamVideoDO famVideoDO = famVideoMapper.selectById(videoHistoryRespVO.getVideoId());
            videoHistoryRespVO.setFamVideoDO(famVideoDO);
        });
        return success(videoHistoryRespVOPageResult);
    }
}
